package cn.com.mybaits;


import cn.com.mybaits.handler.InvocationHandlerMy;

import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 代理工厂
 *
 * @author LiuGuodong
 * @date 2023/09/27
 */
public class MybatisFactory {

    private static final Map<String, Object> BEAN_MAP = new ConcurrentHashMap<>();

    public static Object getBean(String key) {
        return BEAN_MAP.getOrDefault(key, null);
    }

    public static void setBean(String key, Object bean) {
        if (Objects.isNull(key) || Objects.isNull(bean)) {
            return;
        }
        BEAN_MAP.put(key, bean);
    }

    public static Map<String, Object> getMapper() {
        return BEAN_MAP;
    }

    public static <T> T getMapper(Class<T> tClass) throws Exception {
        return (T) Proxy.newProxyInstance(tClass.getClassLoader(), new Class[]{tClass}, new InvocationHandlerMy());
    }
}
